package com.voicechat.firstmet.imcontrol;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.tencent.TIMConversation;
import com.tencent.TIMConversationType;
import com.tencent.TIMFriendGenderType;
import com.tencent.TIMFriendshipManager;
import com.tencent.TIMUserProfile;
import com.tencent.TIMValueCallBack;
import com.voicechat.firstmet.activity.IMChatActivity;
import com.voicechat.firstmet.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 好友或群聊的会话
 */
public class NomalConversation extends Conversation {
    private String TAG = "NomalConversation";

    private TIMConversation conversation;
    //最后一条消息
    private Message lastMessage;


    public NomalConversation(TIMConversation conversation){
        this.conversation = conversation;
        type = conversation.getType();
        identify = conversation.getPeer();
        //getUserMessageOther(identify);
    }
    public void setLastMessage(Message lastMessage) {
        this.lastMessage = lastMessage;
    }

    @Override
    public String getAvatar() {
        switch (type){
            case C2C:
                return avatar;
        }
        return null;
    }

    /**
     * 跳转到聊天界面或会话详情
     *
     * @param context 跳转上下文
     */
    @Override
    public void navToDetail(Context context) {
        IMChatActivity.navToChat(context,identify,getName(),getIsAnchor(),"");
    }
    /**
     * 获取最后一条消息摘要
     */
    @Override
    public String getLastMessageSummary(){
        if (conversation.hasDraft()){
            TextMessage textMessage = new TextMessage(conversation.getDraft());
            if (lastMessage == null || lastMessage.getMessage().timestamp() < conversation.getDraft().getTimestamp()){
                //return App.getContext().getString(R.string.conversation_draft) + textMessage.getSummary();
                return null;
            }else{
                return lastMessage.getSummary();
            }
        }else{
            if (lastMessage == null) return "";
            return lastMessage.getSummary();
        }
    }

    /**
     * 获取名称
     */
    @Override
    public String getName() {
        if(!TextUtils.isEmpty(name)){
        }else {
            getUserMessageOther(identify);
        }
        return name;
    }

    @Override
    public String getIsVip() {
        return isVip;
    }

    @Override
    public String getIsAnchor() {
        return isAnchor;
    }

    @Override
    public String getBirthday() {
        return birthday;
    }

    @Override
    public TIMFriendGenderType getSex() {
        return sex;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    @Override
    public void setIsVip(String isVip) {
        this.isVip = isVip;
    }

    @Override
    public void setIsAnchor(String isAnchor) {
        this.isAnchor = isAnchor;
    }

    @Override
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    @Override
    public String getIdentify() {
        return identify;
    }

    @Override
    public void setSex(TIMFriendGenderType sex) {
        this.sex = sex;
    }

    /**
     * 获取未读消息数量
     */
    @Override
    public long getUnreadNum(){
        if (conversation == null) return 0;
        return conversation.getUnreadMessageNum();
    }

    /**
     * 将所有消息标记为已读
     */
    @Override
    public void readAllMessage(){
        if (conversation != null){
            conversation.setReadMessage();
        }
    }
    /**
     * @method
     * @description 获取别人资料
     * @date: 2018/11/9 11:10
     * @author: ljp
     * @return
     */
    public void getUserMessageOther(String string) {
        //待获取用户资料的用户列表
        List<String> users = new ArrayList<String>();
        users.add(string);
        //获取用户资料
        TIMFriendshipManager.getInstance().getUsersProfile(users, new TIMValueCallBack<List<TIMUserProfile>>() {
            @Override
            public void onError(int code, String desc) {
                Log.e(TAG, "getUsersProfile failed: " + code + " desc");
            }
            @Override
            public void onSuccess(List<TIMUserProfile> result) {
                for (TIMUserProfile res : result) {
                    //Log.e(TAG, "identifier: " + res.getIdentifier() + " nickName: " + res.getNickName()
                      //      + " getFaceUrl: " + res.getFaceUrl()+"---getGender--"+res.getGender());
                    try {
                        if(identify.equals(res.getIdentifier())) {
                            if (!TextUtils.isEmpty(res.getNickName())) {
                                setName(res.getNickName());
                                setSex(res.getGender());
                                /*name = res.getNickName();
                                sex = res.getGender();*/
                                String[] strings = Utils.spitInfo(res.getFaceUrl());
                                // 头像地址 + 是否会员 + 是否主播 + 生日
                               /* avatar = strings[0];
                                isVip = strings[1];
                                isAnchor = strings[2];
                                birthday = strings[3];*/
                                setAvatar(strings[0]);
                                setBirthday(strings[3]);
                                setIsVip(strings[1]);
                                setIsAnchor(strings[2]);
                            }
                        }
                    }catch (RuntimeException e){

                    }

                }
            }
        });
    }
    /**
     * 获取最后一条消息的时间
     */
    @Override
    public long getLastMessageTime() {
        if (conversation.hasDraft()){
            if (lastMessage == null || lastMessage.getMessage().timestamp() < conversation.getDraft().getTimestamp()){
                return conversation.getDraft().getTimestamp();
            }else{
                return lastMessage.getMessage().timestamp();
            }
        }
        if (lastMessage == null) return 0;
        return lastMessage.getMessage().timestamp();
    }

    /**
     * 获取会话类型
     */
    public TIMConversationType getType(){
        return conversation.getType();
    }

    @Override
    public String toString() {
        return "NomalConversation{" +
                "identify='" + identify + '\'' +
                ", type=" + type +
                ", name='" + name + '\'' +
                ", avatar='" + avatar + '\'' +
                ", isVip='" + isVip + '\'' +
                ", isAnchor='" + isAnchor + '\'' +
                ", birthday='" + birthday + '\'' +
                ", sex=" + sex +
                '}';
    }
}
